// Anti-cheat measures
(function() {
    try {
        // Detect and prevent DevTools
        const devToolsDetector = {
            isOpen: false,
            orientation: undefined
        };

        // Check if device is mobile
        const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);

        const emitChecks = () => {
            try {
                // Check source viewing
                const sourceCheck = new Error().stack;
                if (sourceCheck.includes('chrome-extension://') || sourceCheck.includes('firefox-extension://')) {
                    return;
                }
            } catch (error) {
                reportError(error);
            }
        };

        // Check if should enforce anti-cheat
        const shouldEnforceAntiCheat = () => {
            try {
                return !window.top;
            } catch (error) {
                return true;
            }
        };

        // Detect DevTools by window size
        const detectDevTools = ({ emitEvents = true } = {}) => {
            try {
                // Skip size check on mobile devices
                if (isMobile) {
                    return;
                }

                const widthThreshold = window.outerWidth - window.innerWidth > 160;
                const heightThreshold = window.outerHeight - window.innerHeight > 160;
                const orientation = widthThreshold ? 'vertical' : 'horizontal';

                if (
                    widthThreshold ||
                    heightThreshold
                ) {
                    devToolsDetector.isOpen = true;
                    devToolsDetector.orientation = orientation;

                    // Refresh page if anti-cheat should be enforced
                    if (shouldEnforceAntiCheat()) {
                        window.location.reload();
                    }
                } else {
                    devToolsDetector.isOpen = false;
                    devToolsDetector.orientation = undefined;
                }
            } catch (error) {
                reportError(error);
            }
        };

        // Prevent right-click
        window.addEventListener('contextmenu', function (e) {
            e.preventDefault();
        }, false);

        // Prevent keyboard shortcuts
        window.addEventListener('keydown', function(e) {
            try {
                // Common dev tools shortcuts
                if (
                    // Windows/Linux shortcuts
                    (e.ctrlKey && e.shiftKey && (e.key === 'I' || e.key === 'J' || e.key === 'C')) || // Dev tools shortcuts
                    (e.ctrlKey && e.key === 'U') || // View source
                    (e.key === 'F12') || // Dev tools
                    // Mac shortcuts
                    (e.metaKey && e.altKey && (e.key === 'i' || e.key === 'j' || e.key === 'c')) || // Dev tools shortcuts
                    (e.metaKey && e.key === 'u') // View source
                ) {
                    e.preventDefault();
                    e.stopPropagation();

                    // Refresh page if anti-cheat should be enforced
                    if (shouldEnforceAntiCheat()) {
                        window.location.reload();
                    }

                    return false;
                }
            } catch (error) {
                reportError(error);
            }
        }, true);

        // Additional keyboard event prevention
        window.addEventListener('keyup', function(e) {
            try {
                if (
                    (e.metaKey && e.altKey) || // Prevent any Mac Command+Option combinations
                    (e.ctrlKey && e.shiftKey) // Prevent any Ctrl+Shift combinations
                ) {
                    e.preventDefault();
                    e.stopPropagation();

                    // Refresh page if anti-cheat should be enforced
                    if (shouldEnforceAntiCheat()) {
                        window.location.reload();
                    }

                    return false;
                }
            } catch (error) {
                reportError(error);
            }
        }, true);

        // Check for debugger every 500ms
        setInterval(() => {
            try {
                detectDevTools();
                emitChecks();
            } catch (error) {
                reportError(error);
            }
        }, 500);

        // Detect page visibility changes
        document.addEventListener('visibilitychange', () => {
            try {
                if (document.hidden) {
                    detectDevTools();
                }
            } catch (error) {
                reportError(error);
            }
        });

        // Prevent debugging
        const debug = function() {
            try {
                debugger;
            } catch (error) {
                reportError(error);
            }
        };
        debug();

        // Additional protection against dev tools
        (() => {
            try {
                function devToolsChecker() {
                    const d = new Date();
                    debugger;
                    const timeTaken = new Date() - d;
                    if (timeTaken > 100) {
                        // Refresh page if anti-cheat should be enforced
                        if (shouldEnforceAntiCheat() && !isMobile) {  // Skip on mobile
                            window.location.reload();
                        }
                        return;
                    }
                }

                // Run the checker every 500ms
                setInterval(devToolsChecker, 500);
            } catch (error) {
                reportError(error);
            }
        })();

    } catch (error) {
        reportError(error);
    }
})();
